package org.learn.service.impl;

import org.learn.entity.User;
import org.learn.entity.UserList;
import org.learn.mapper.UserMapper;
import org.learn.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<UserList> findAll(){
        return this.userMapper.findAll();
    }

    @Override
    public Integer insert(Integer id,String name) {
        return this.userMapper.insert(id,name);
    }
    @Transactional(rollbackFor = Exception.class) //所有异常都会进行事务回滚
    public Integer delete(Integer id){
        return userMapper.delete(id);
    }

    public Integer put(Integer id,String name){
        return this.userMapper.put(id,name);
    }

    @Override
    public Integer login(String username, String password) {
        User user = userMapper.login(username, password);
        if(Objects.isNull(user)) return 0;
        return 1;
    }
}
